~ chicken-core (chicken-5) /manual/Module (chicken port)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken port)
5
6This module contains various extended port definitions.
7
8All errors related to failing port operations will signal a condition
9of kind {{exn}}.
10
11* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
12 change errno will produce a condition object with an {{errno}}
13 property, which can be accessed with
14 {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
15
16=== Port attributes
17
18==== port-name
19
20<procedure>(port-name [PORT])</procedure>
21
22Fetch filename from {{PORT}}. This returns the filename that was used to open
23this file. Returns a special tag string, enclosed into parentheses for
24non-file ports. {{PORT}} defaults to the value of {{(current-input-port)}}.
25
26
27==== port-position
28
29<procedure>(port-position [PORT])</procedure>
30
31Returns the current position of {{PORT}} as two values: row and column number.
32If the port does not support such an operation an error is signaled. This
33procedure is currently only available for input ports. {{PORT}} defaults to the
34value of {{(current-input-port)}}.
35
36
37==== set-port-name!
38
39<procedure>(set-port-name! PORT STRING)</procedure>
40
41Sets the name of {{PORT}} to {{STRING}}.
42
43
44=== Setting the file buffering mode
45
46==== set-buffering-mode!
47
48<procedure>(set-buffering-mode! PORT MODE [BUFSIZE])</procedure>
49
50Sets the buffering-mode for the file associated with {{PORT}} to
51{{MODE}}, which should be one of the keywords {{#:full}},
52{{#:line}} or {{#:none}}. If {{BUFSIZE}} is specified it
53determines the size of the buffer to be used (if any).
54
55This procedure doesn't work on custom ports, such as those created with {{make-input-port}} or {{make-output-port}}.
56
57=== Terminal ports
58
59==== terminal-name
60
61<procedure>(terminal-name PORT)</procedure>
62
63Returns the name of the terminal that is connected to {{PORT}}.
64
65On Windows, this procedure always raises an exception.
66
67==== terminal-port?
68
69<procedure>(terminal-port? PORT)</procedure>
70
71Returns {{#t}} if {{PORT}} is connected to a terminal and
72{{#f}} otherwise.
73
74
75==== terminal-size
76
77<procedure>(terminal-size PORT)</procedure>
78
79Returns two values, the number of rows and columns of the terminal
80that is connected to {{PORT}} or {{0}}, {{0}} if the terminal size can
81not be obtained.
82
83On Windows, this procedure always raises an exception.
84
85
86=== Input/output port extensions
87
88==== with-output-to-port
89
90<procedure>(with-output-to-port PORT THUNK)</procedure>
91
92Call procedure {{THUNK}} with the current output-port temporarily
93bound to {{PORT}}.
94
95==== make-input-port
96
97<procedure>(make-input-port READ-CHAR CHAR-READY? CLOSE [PEEK-CHAR [READ-STRING! [READ-LINE]]])</procedure>
98
99Returns a custom input port. Common operations on this port are
100handled by the given parameters, which should be procedures of no
101arguments. The following arguments are all different kinds of reader
102procedures:
103
104* {{READ-CHAR}} is the most fundamental reader, and must always be
105present. It is a thunk which is called when the next character is
106to be read and it should return a character or {{#!eof}}.
107* {{CHAR-READY?}} is a thunk which is called when {{char-ready?}}
108is called on this port and should return {{#t}} or {{#f}}.
109* {{CLOSE}} is a thunk which is called when the port is closed.
110* {{PEEK-CHAR}} is a thunk which is called when {{peek-char}} is
111called on this port and should return a character or {{#!eof}}. If it
112is not provided or {{#f}}, {{READ-CHAR}} will be used instead and the
113created port object handles peeking automatically (by calling {{READ}}
114and buffering the character).
115* {{READ-STRING!}} is called when {{read-string!}} is called (or the
116higher-level non-mutating {{read-string}}). It will be invoked with 4
117arguments: the port created by {{make-input-port}}, the number of
118bytes to read, a string (or sometimes a blob) to read into (which may be
119assumed to be big enough to hold the data) and the offset into the
120buffer at which to put the data to read. It should return the number
121of bytes that have successfully been read, which should always be
122equal to the requested bytes unless EOF was hit, in which case it can
123be less. If this procedure is not provided or {{#f}}, the buffer will
124be filled by repeated reads to {{READ-CHAR}}.
125* {{READ-LINE}} is called when {{read-line}} is called. It will be
126invoked with two arguments: the port created by {{make-input-port}}
127and the maximum number of characters to read (or {{#f}}). If this
128procedure is not provided or {{#f}}, the buffer will be filled by
129repeated reads to {{READ-CHAR}}.
130
131All the optional procedures except for {{PEEK-CHAR}} are responsible
132for updating the port's position, which currently can only be done via
133low-level slot accessors like {{##sys#setslot}}; slot 4 is the row
134number (ie, the line) and slot 5 is the column number (ie, the
135character on the line). If the port's positions are not updated,
136{{port-position}} won't work.
137
138
139==== make-output-port
140
141<procedure>(make-output-port WRITE CLOSE [FLUSH])</procedure>
142
143Returns a custom output port. Common operations on this port are handled
144by the given parameters, which should be procedures. {{WRITE}} is
145called when output is sent to the port and receives a single argument,
146a string. {{CLOSE}} is called when the port is closed and should
147be a procedure of no arguments. {{FLUSH}} (if provided) is called
148for flushing the output port.
149
150
151==== with-error-output-to-port
152
153<procedure>(with-error-output-to-port PORT THUNK)</procedure>
154
155Call procedure {{THUNK}} with the current error output-port
156temporarily bound to {{PORT}}.
157
158
159==== with-input-from-port
160
161<procedure>(with-input-from-port PORT THUNK)</procedure>
162
163Call procedure {{THUNK}} with the current input-port temporarily
164bound to {{PORT}}.
165
166
167=== String-port extensions
168
169==== call-with-input-string
170
171<procedure>(call-with-input-string STRING PROC)</procedure>
172
173Calls the procedure {{PROC}} with a single argument that is a
174string-input-port with the contents of {{STRING}}.
175
176
177==== call-with-output-string
178
179<procedure>(call-with-output-string PROC)</procedure>
180
181Calls the procedure {{PROC}} with a single argument that is a
182string-output-port. Returns the accumulated output-string.
183
184
185==== with-input-from-string
186
187<procedure>(with-input-from-string STRING THUNK)</procedure>
188
189Call procedure {{THUNK}} with the current input-port temporarily
190bound to an input-string-port with the contents of {{STRING}}.
191
192
193==== with-output-to-string
194
195<procedure>(with-output-to-string THUNK)</procedure>
196
197Call procedure {{THUNK}} with the current output-port temporarily
198bound to a string-output-port and return the accumulated output string.
199
200==== with-error-output-to-string
201
202<procedure>(with-error-output-to-string THUNK)</procedure>
203
204Call procedure {{THUNK}} with the current error output-port
205temporarily bound to a string-output-port and return the accumulated
206output string.
207
208
209=== Port iterators
210
211==== port-for-each
212
213<procedure>(port-for-each FN THUNK)</procedure>
214
215Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, discarding the results.
216
217==== port-map
218
219<procedure>(port-map FN THUNK)</procedure>
220
221Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, returning a list of the collected results.
222
223==== port-fold
224
225<procedure>(port-fold FN ACC THUNK)</procedure>
226
227Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}}, (typically {{read}}) passing the {{ACC}} value as the second argument. The {{FN}} result becomes the new {{ACC}} value. When {{THUNK}} returns {{#!eof}}, the last {{FN}} result is returned.
228
229==== copy-port
230
231<procedure>(copy-port FROM TO [READ [WRITE]])</procedure>
232
233Reads all remaining data from port {{FROM}} using the reader procedure
234{{READ}} and writes it to port {{TO}} using the writer procedure
235{{WRITE}}. {{READ}} defaults to {{read-char}} and {{WRITE}} to
236{{write-char}}. Note that this procedure does not check {{FROM}} and
237{{TO}} for being ports, so the reader and writer procedures may
238perform arbitrary operations as long as they can be invoked
239as {{(READ FROM)}} and {{(WRITE X TO)}}, respectively.
240{{copy-port}} returns an undefined value.
241
242{{copy-port}} was introduced in CHICKEN 4.6.0.
243
244=== Funky ports
245
246==== make-bidirectional-port
247
248<procedure>(make-bidirectional-port INPUT-PORT OUTPUT-PORT)</procedure>
249
250Returns a joint input/output port that proxies port operations to the
251given {{INPUT-PORT}} and {{OUTPUT-PORT}}, respectively. This port
252satisfies both {{input-port?}} and {{output-port?}}, and its two
253directions may be closed independently.
254
255==== make-broadcast-port
256
257<procedure>(make-broadcast-port PORT ...)</procedure>
258
259Returns a custom output port that emits everything written into it to
260the ports given as {{PORT ...}}. Closing the broadcast port does not close
261any of the argument ports.
262
263==== make-concatenated-port
264
265<procedure>(make-concatenated-port PORT1 PORT2 ...)</procedure>
266
267Returns a custom input port that reads its input from {{PORT1}}, until it
268is empty, then from {{PORT2}} and so on. Closing the concatenated port
269does not close any of the argument ports.
270
271
272---
273Previous: [[Module (chicken plist)]]
274
275Next: [[Module (chicken pretty-print)]]